package com.example.starseas_phone;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.starseas_phone.Entity.Dept;
import com.example.starseas_phone.Entity.Students;
import com.example.starseas_phone.Entity.Teachers;

import java.util.ArrayList;


public class MainF4 extends Fragment implements View.OnClickListener {

    private ImageView mainf4_touxiang;
    private TextView mainf4_name;
    private TextView mainf4_CollegeName;
    private TextView mainf4_ProfessionalName;

    private ImageView mainf4_imformation;

    private TextView mainf4_tcdl;
    private TextView mainf4_xgmm;

    private LinearLayout mainf4_dept_linear;
    private LinearLayout mainf4_linear;
    private TextView mainf4_dept_name;



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.mainf4,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initid();
        if (Sto.State==1){
            mainf4_dept_linear.setVisibility(View.GONE);
            mainf4_linear.setVisibility(View.VISIBLE);
            Stu_initview();
        }else {
            if (Sto.teachers.getPositionId()==6){
                mainf4_dept_linear.setVisibility(View.VISIBLE);
                mainf4_linear.setVisibility(View.GONE);
                Dept_initview();
            }else {
                mainf4_dept_linear.setVisibility(View.GONE);
                mainf4_linear.setVisibility(View.VISIBLE);
                Teacher_initview();
            }
        }
    }

    private void initid(){
        mainf4_touxiang = (ImageView) getActivity().findViewById(R.id.mainf4_touxiang);
        mainf4_name = (TextView) getActivity().findViewById(R.id.mainf4_name);
        mainf4_CollegeName = (TextView) getActivity().findViewById(R.id.mainf4_CollegeName);
        mainf4_ProfessionalName = (TextView) getActivity().findViewById(R.id.mainf4_ProfessionalName);

        mainf4_imformation = (ImageView) getActivity().findViewById(R.id.mainf4_imformation);
        mainf4_imformation.setOnClickListener(this);

        mainf4_tcdl = (TextView) getActivity().findViewById(R.id.mainf4_tcdl);
        mainf4_tcdl.setOnClickListener(this);
        mainf4_xgmm = (TextView) getActivity().findViewById(R.id.mainf4_xgmm);
        mainf4_xgmm.setOnClickListener(this);

        mainf4_dept_linear = (LinearLayout) getActivity().findViewById(R.id.mainf4_dept_linear);
        mainf4_linear = (LinearLayout) getActivity().findViewById(R.id.mainf4_linear);
        mainf4_dept_name = (TextView) getActivity().findViewById(R.id.mainf4_dept_name);
    }

    private void Stu_initview(){

        //修改头像
        if (Sto.students.getStuSex().equals("男")){
            mainf4_touxiang.setBackgroundResource(R.drawable.man_student);
        }else {
            mainf4_touxiang.setBackgroundResource(R.drawable.women_student);
        }

        //修改名字
        mainf4_name.setText(Sto.students.getStuName());
        //修改学院
        mainf4_CollegeName.setText(Sto.students.getCollegeName());
        //修改专业
        mainf4_ProfessionalName.setText(Sto.students.getProfessionalName());
    }

    private void Teacher_initview(){

        //修改头像
        if (Sto.teachers.getWorkerSex().equals("男")){
            mainf4_touxiang.setBackgroundResource(R.drawable.man_teacher);
        }else {
            mainf4_touxiang.setBackgroundResource(R.drawable.women_teacher);
        }

        //修改名字
        mainf4_name.setText(Sto.teachers.getWorkerName());
        //修改学院
        mainf4_CollegeName.setText(Sto.teachers.getCollegeName());
        //修改专业
        mainf4_ProfessionalName.setText(Sto.teachers.getProfessionalName());
    }

    private void Dept_initview(){
        mainf4_dept_name.setText(Sto.dept.getWorkerName());
        //修改头像
        if (Sto.dept.getWorkerSex().equals("男")){
            mainf4_touxiang.setBackgroundResource(R.drawable.man_teacher);
        }else {
            mainf4_touxiang.setBackgroundResource(R.drawable.women_teacher);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.mainf4_imformation:
                Intent intent = new Intent(getActivity(), ImformationActivity.class);
                intent.putExtra("id",Sto.teachers.getPositionId()+"");
                startActivity(intent);
                break;
            case R.id.mainf4_tcdl:
                //首先添加一个edit实例，然后通过dialog设置显示
                AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
                dialog.setTitle("确定要退出登录吗?");
                dialog.setCancelable(false);
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent1  = new Intent(getActivity(),LoginActivity.class);
                        startActivity(intent1);
                        MainF1.list_schedule.clear();
                        Sto.students = new Students();
                        Sto.teachers = new Teachers();
                        Sto.dept = new Dept();
                        getActivity().finish();
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
                break;
            case R.id.mainf4_xgmm:
                Intent intent2 = new Intent(getActivity(),Change_pwdActivity.class);
                Log.d("Sto.State",Sto.State+"");
                String id;
                if (Sto.State==1){
                    id = Sto.students.getStuId()+"";
                }else {
                    if (Sto.teachers.getPositionId()==6){
                        id = Sto.dept.getWorkerId()+"";
                    }else {
                        id = Sto.teachers.getWorkerId()+"";
                    }
                }
                intent2.putExtra("id",id);
                startActivity(intent2);
        }
    }

    @Override
    public void onResume() {
        if (Sto.State==0){
            Intent intent = new Intent(getActivity(),LoginActivity.class);
            startActivity(intent);
            MainF1.list_schedule.clear();
            Sto.students = new Students();
            Sto.teachers = new Teachers();
            Sto.dept = new Dept();
            getActivity().finish();
        }
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}